#region Import Declarations

using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web;
using CommunityServer;
using CommunityServer.Components;
using CommunityServer.Blogs.Components;
using CookComputing.XmlRpc;

#endregion

namespace CommunityServerStuff.Handlers
{
    /// <summary>
    /// Updated MetaWeblogHandler that allows objects to be uploaded throught
    /// the newMediaObject function on the MetaWeblog API.
    /// </summary>
    [Obsolete("This class has been replaced by the new MetaWeblogHandler in CS2007. It is included here for compatibility. It will be removed from CSStuff 2.1", true)]
    class MetaWeblogHandler : MetaWeblog
    {

        #region Structures

        #region MediaObjectUrl

        /// <summary>
        /// 
        /// </summary>
        public struct MediaObjectUrl
        {
            public string url;
        }

        #endregion

        #region MediaObject

        /// <summary>
        /// 
        /// </summary>
        public struct MediaObject
        {
            public string name;
            public string type;
            public byte[] bits;
        }

        #endregion

        #endregion

        #region Methods

        #region newMediaObject

        /// <summary>
        /// Post a media object.
        /// </summary>
        /// <param name="blogid">The blogid.</param>
        /// <param name="username">The username.</param>
        /// <param name="password">The password.</param>
        /// <param name="mediaObject">The media object.</param>
        /// <returns>MediaObjectUrl defining the url of the media</returns>
        [XmlRpcMethod("metaWeblog.newMediaObject", Description = "Add a media object to a post using the metaWeblog API. Returns media url as a string.")]
        public MediaObjectUrl newMediaObject(string blogid, string username, string password, MediaObject mediaObject)
        {

            if (ValidateUser(username, password))
            {
                try
                {
                    CSContext context1 = CSContext.Current;
                    Weblog weblog1 = Weblogs.GetWeblog(blogid, true);

                    mediaObject.name = mediaObject.name.Substring(mediaObject.name.LastIndexOf("/") +1);

                    string mediaFolder = String.Format("/images/{0}/", weblog1.ApplicationKey);
                    string physicalFileName = HttpContext.Current.Request.PhysicalApplicationPath + mediaFolder + mediaObject.name;
                    string virtualFileName = CSContext.Current.SiteSettings.SiteUrl + mediaFolder + mediaObject.name;
                    //EventLogs.Write("mediaFolder: " + mediaFolder, "newMediaObject", 1, EventType.Error);
                    //EventLogs.Write("physicalFileName: " + physicalFileName, "newMediaObject", 1, EventType.Error);
                    //EventLogs.Write("virtualFileName: " + virtualFileName, "newMediaObject", 1, EventType.Error);
                    //EventLogs.Write("physicalApplicationPath: " + HttpContext.Current.Request.PhysicalApplicationPath, "newMediaObject", 1, EventType.Error);
                    


                    if (!Directory.Exists(Path.GetDirectoryName(physicalFileName)))
                        CommunityServer.Components.WebDirectory.Create(mediaFolder);
                    File.WriteAllBytes(physicalFileName, mediaObject.bits);

                    MediaObjectUrl mediaObjectUrl = new MediaObjectUrl();
                    mediaObjectUrl.url = virtualFileName;
                    return mediaObjectUrl;
                }
                catch (Exception ex)
                {
                    CSException csEx = new CSException(CSExceptionType.UnknownError, "The handler RobertMcLaws.CommunityServerStuff.Handlers.MetaWeblogHandler failed." + Environment.NewLine + ex.Message, (ex.InnerException != null ? ex.InnerException : ex));
                    csEx.Log();
                }
            }
            else
            {
                throw new XmlRpcFaultException(0, "You have no right to do that.");
            }
            return new MediaObjectUrl();
        }

        #endregion

        #region ValidateUser

        /// <summary>
        /// 
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        private bool ValidateUser(string username, string password)
        {
            try
            {
                User user1 = new User();
                user1.Username = username;
                user1.Password = password;
                LoginUserStatus status1 = Users.ValidUser(user1);
                if (status1 == LoginUserStatus.Success)
                {
                    CSContext.Current.User = Users.GetUser(-1, user1.Username, false, false);
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }

        #endregion

        #endregion

    }
}
